﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
using System.Runtime.Serialization;
using System.Xml;
using System.ServiceModel.Syndication;
using System.Data;
using System.Collections.ObjectModel;

namespace OpenSunTracker.RestClient.Extensions
{
    public static class RestExtensions
    {

        public static List<LogEntry> ReadRss(this string uri)
        {
            var webResponse = WebRequest.Create(uri).GetResponse();
            if (webResponse == null)
                return null;
            var ds = new DataSet();
            ds.ReadXml(webResponse.GetResponseStream());

            var news = (from row in ds.Tables["item"].AsEnumerable()
                        select new LogEntry
                        {
                            Title = row.Field<string>("title"),
                            PublicationDate = row.Field<string>("pubDate"),
                        }).ToList();
            return news;
        }

        public static WebResponse ExecuteWriteRequestString(this string uri, string httpMethod, string content)
        {
            var req = WebRequest.Create(uri);
            req.Method = httpMethod;

            byte[] data = Encoding.UTF8.GetBytes(content);

            req.ContentType = "text/plain";
            req.ContentLength = data.Length;

            Stream requestStream = req.GetRequestStream();
            requestStream.Write(data, 0, data.Length);
            requestStream.Close();

            return req.GetResponse();
        }

        public static WebResponse ExecuteWriteRequest<T>(this string uri, string httpMethod, T obj)
        {
            var req = WebRequest.Create(uri);
            req.Method = httpMethod;

            WriteContent2Request<T>(obj, req);

            return req.GetResponse();
        }

        public static void WriteContent2Request<T>(this T obj, WebRequest req)
        {
            var targetType = typeof(T);
            //var serializer = new DataContractJsonSerializer(targetType);
            var serializer = new DataContractSerializer(targetType);
            var mem = new MemoryStream();
            serializer.WriteObject(mem, obj);
            var postData = Encoding.UTF8.GetString(mem.ToArray());

            byte[] data = Encoding.UTF8.GetBytes(postData);

            req.ContentType = "text/xml";
            req.ContentLength = data.Length;

            Stream requestStream = req.GetRequestStream();
            requestStream.Write(data, 0, data.Length);
            requestStream.Close();
        }

        public static T Try2ConvertResponse<T>(WebResponse response)
        {
            var targetType = typeof(T);
            var serializer = new DataContractSerializer(targetType);

            //new DataContractJsonSerializer(typeof(string)).ReadObject(response.GetResponseStream()) = "45035846-0e99-4a2d-ad07-edfb5a34c6b9"

            return (T)serializer.ReadObject(response.GetResponseStream());
        }

        public static WebResponse ExecuteReadRequest(this string uri)
        {
            var req = WebRequest.Create(uri);
            req.Method = "GET";
            req.ContentType = "text/xml";


            return req.GetResponse();
        }

        public static WebResponse ExecuteReadRequest<T>(string uri, T obj)
        {
            var req = WebRequest.Create(uri);
            req.Method = "GET";
            req.ContentType = "text/xml";

            WriteContent2Request<T>(obj, req);

            return req.GetResponse();
        }

        public static void WriteContent2Request<T>(this  WebRequest req, T obj)
        {
            var targetType = typeof(T);
            var serializer = new DataContractSerializer(targetType);
            var mem = new MemoryStream();
            serializer.WriteObject(mem, obj);
            var postData = Encoding.UTF8.GetString(mem.ToArray());

            byte[] data = Encoding.UTF8.GetBytes(postData);

            req.ContentType = "text/xml";
            req.ContentLength = data.Length;

            Stream requestStream = req.GetRequestStream();
            requestStream.Write(data, 0, data.Length);
            requestStream.Close();
        }


    }
}
